package com.cennavi.minemapdemo.activity.ue;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.activity.BaseActivty;
import com.cennavi.minemapdemo.util.Config;
import com.cennavi.minemapdemo.util.DensityUtil;
import com.minedata.minemap.MinemapAccountManager;
import com.minedata.minemap.camera.CameraPosition;
import com.minedata.minemap.camera.CameraUpdateFactory;
import com.minedata.minemap.geometry.LatLng;
import com.minedata.minemap.map.MapView;
import com.minedata.minemap.map.MineMap;


public class UISettingActivty extends BaseActivty {

    //地图视图
    private MapView mapView;
    //地图操作类
    private MineMap mineMap;
    private Context mContext;

    private CheckBox zoom;
    private CheckBox scroll;
    private CheckBox rotate;
    private CheckBox overlook;
    private CheckBox compass;
    private CheckBox logo;
    private CheckBox attribute;
    private CheckBox doubleTap;
    private CheckBox allGesture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MinemapAccountManager.getInstance(this, Config.MAP_TOK, Config.OFFLINE_TOK, Config.MAP_ID);

        setContentView(R.layout.activity_ue_ui_setting);

        mContext = this;
        mapView = findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);

        initMap();
        initView();

    }

    private void initMap() {
        mapView.setApiBaseUrl(Config.API_BASE_URL);
        mapView.setApiUrl(Config.API_URL);
        mapView.setSpriteImageUrl(Config.SPRITE_GLYPHS_URL);
        mapView.addMapRenderCallback(new MapView.OnMapReadyListener() {
            @SuppressLint("RtlHardcoded")
            @Override
            public void onMapReady(MineMap oMineMap) {
                mineMap = oMineMap;
                mineMap.getUiSettings().setZoomControlsEnabled(false);
                mineMap.getUiSettings().setRotateGesturesEnabled(true);// 是否可以旋转地图
                mineMap.getUiSettings().setTiltGesturesEnabled(true);
                mineMap.getUiSettings().setLogoEnabled(true);
                mineMap.setStyleUrl(Config.BASE_URL + Config.MAP_ID, new MineMap.OnStyleLoadedListener() {
                    @Override
                    public void onStyleLoaded(String s) {
                        mineMap.setTrafficShow(false); //是否展示交通路况信息
                    }
                });
                //设置指南针是否显示，默认是显示
                mineMap.getUiSettings().setCompassEnabled(true);
                mineMap.getUiSettings().setCompassGravity(Gravity.LEFT);
//                mineMap.getUiSettings().setCompassImage(getResources().getDrawable(R.mipmap.icon_compass));
                mineMap.getUiSettings().setCompassMargins(DensityUtil.dip2px(mContext, 11), DensityUtil.dip2px(mContext, 80),
                        0, 0);
                mineMap.setCameraPosition(
                        new CameraPosition.Builder().target(new LatLng(Config.mapCentrelat, Config.mapCentrelon)).zoom(14)
                                .tilt(0)//设定倾斜角度
                                .bearing(0) //摄像机指向的方向,从北部顺时针方向设置
                                .build());
                mineMap.setMaxZoomPreference(17);//设置最高展示级别，最高为17级别
                mineMap.setMinZoomPreference(3);//设置最低的展示级别
            }
        });

    }

    private void initView() {

        zoom = findViewById(R.id.zoom);
        scroll = findViewById(R.id.scroll);
        rotate = findViewById(R.id.rotate);
        overlook = findViewById(R.id.overlook);
        compass = findViewById(R.id.compass);
        logo = findViewById(R.id.logo);
        attribute = findViewById(R.id.attribute);
        doubleTap = findViewById(R.id.doubleTap);
        allGesture = findViewById(R.id.allGesture);

        final ImageView zoomIn = findViewById(R.id.zoomin);
        final ImageView zoomOut = findViewById(R.id.zoomout);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (view.equals(zoom)) {

                    setZoomState();
                } else if (view.equals(scroll)) {

                    setScrollState();
                } else if (view.equals(rotate)) {

                    setRotateState();

                } else if (view.equals(overlook)) {

                    setOverlookState();

                } else if (view.equals(compass)) {

                    setCompassState();

                } else if (view.equals(logo)) {

                    setLogoState();
                } else if (view.equals(zoomIn)) {

                    zoomIn(view);
                } else if (view.equals(zoomOut)) {

                    zoomOut(view);
                } else if (view.equals(attribute)) {
                    setAttributeState();
                } else if (view.equals(doubleTap)) {
                    setDoubleTapState();
                } else if (view.equals(allGesture)) {
                    setAllGestureState();
                }
            }
        };

        zoom.setOnClickListener(listener);
        scroll.setOnClickListener(listener);
        rotate.setOnClickListener(listener);
        overlook.setOnClickListener(listener);
        compass.setOnClickListener(listener);
        logo.setOnClickListener(listener);
        attribute.setOnClickListener(listener);
        doubleTap.setOnClickListener(listener);
        allGesture.setOnClickListener(listener);
        zoomIn.setOnClickListener(listener);
        zoomOut.setOnClickListener(listener);

    }

    private void setAllGestureState() {
        mineMap.getUiSettings().setAllGesturesEnabled(allGesture.isChecked());
    }

    private void setDoubleTapState() {
        mineMap.getUiSettings().setDoubleTapGesturesEnabled(doubleTap.isChecked());
    }

    private void setAttributeState() {
        mineMap.getUiSettings().setAttributionEnabled(attribute.isChecked());
    }


    public void zoomIn(View view) {
        if (mineMap != null) {
            mineMap.moveCamera(CameraUpdateFactory.zoomIn());
        }
    }

    public void zoomOut(View view) {
        if (mineMap != null) {
            mineMap.moveCamera(CameraUpdateFactory.zoomOut());
        }
    }

    /**
     * 设置缩放状态
     */
    private void setZoomState() {

        mineMap.getUiSettings().setZoomGesturesEnabled(zoom.isChecked());
    }

    /**
     * 设置平移状态
     */
    private void setScrollState() {

        mineMap.getUiSettings().setScrollGesturesEnabled(scroll.isChecked());
    }

    /**
     * 设置旋转状态
     */
    private void setRotateState() {

        mineMap.getUiSettings().setRotateGesturesEnabled(rotate.isChecked());
    }

    /**
     * 设置俯仰状态
     */
    private void setOverlookState() {

        mineMap.getUiSettings().setTiltGesturesEnabled(overlook.isChecked());
    }

    /**
     * 设置指南针的状态
     */
    private void setCompassState() {

        mineMap.getUiSettings().setCompassEnabled(compass.isChecked());
    }

    /**
     * 设置logo的状态
     */
    private void setLogoState() {

        mineMap.getUiSettings().setLogoEnabled(logo.isChecked());

    }


    //地图生命周期
    @Override
    protected void onStart() {
        super.onStart();
        if (mapView != null)
            mapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mapView != null)
            mapView.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mapView != null)
            mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mapView != null)
            mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mapView != null)
            mapView.onDestroy();
    }
}
